﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using System.Text;
namespace Background.Controllers
{
    public class HelperCenterCategoryController : Common.Background.Web.Controllers.BackgroundBaseController
    {
        IHelperCenterCategoryRepository iHelperCenterCategoryRepository = null;
        public HelperCenterCategoryController()
        {
            iHelperCenterCategoryRepository = new HelperCenterCategoryRepository();
        }
        //
        // GET: /HelperCategory/
        public ActionResult Index()
        {
            GetCategory();
            return View();
        }

        //
        // GET: /HelperCategory/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /HelperCategory/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            HelperCenterCategory entity = new HelperCenterCategory();
            TryUpdateModel(entity);
            if (entity.IsValid)
            {
                iHelperCenterCategoryRepository.Insert(entity);
                HelperCenterCategoryRepository.ReLoad();
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }

        //
        // GET: /HelperCategory/Edit/5
        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
                return AlertToUrl("参数非法", "Index");
            var model = iHelperCenterCategoryRepository.Find(id.Value);
            return View(model);
        }

        //
        // POST: /HelperCategory/Edit/5
        [HttpPost]
        public ActionResult Edit(FormCollection collection)
        {
            HelperCenterCategory entity = new HelperCenterCategory();
            TryUpdateModel(entity);
            if (entity.IsValid)
            {

                entity.ModifyBefore += delegate
                {
                    entity.Log = new Common.Background.Entity.WebEntityLogs("帮助中心", "信息从" + iHelperCenterCategoryRepository.Find(entity.HelpCategoryID).Name + "<=修改成了=>" + entity.Name, "zzl");
                };
                entity.ModifyAfter += new HelperCenterCategoryRepository().InsertLog;
                iHelperCenterCategoryRepository.Update(entity);
                HelperCenterCategoryRepository.ReLoad(); //重新加载树
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }

        //
        // GET: /HelperCategory/Delete/5
        public ActionResult Delete(string id)
        {
            List<int> arrID = new List<int>();
            id.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(i =>
            {
                arrID.Add(Convert.ToInt32(i));
            });
            arrID.ForEach(i =>
            {
                iHelperCenterCategoryRepository.Delete(new HelperCenterCategory { HelpCategoryID = i });
            });
            HelperCenterCategoryRepository.ReLoad(); //重新加载树
            return AlertToUrl("Index");
        }

        #region 区域树结构
        #region 分类树结构
        /// <summary>
        /// 得到分类
        /// </summary>
        public void GetCategory()
        {
            HelperCenterCategory common_Area = iHelperCenterCategoryRepository.GetHelperCenterCategoryTree();
            StringBuilder html = new StringBuilder();
            html.Append("<p><ul id='containerul'>");
            this.GetSubCategory(html, common_Area, 0);
            html.Append("</ul></p>");
            ViewData["CategoryData"] = html.ToString();
        }

        /// <summary>
        /// 通过递归拼树形结构
        /// </summary>
        /// <param name="html">树形Html字符串</param>
        /// <param name="category">树形商品分类对象</param>
        private void GetSubCategory(StringBuilder html, HelperCenterCategory common_Area, int num)
        {
            num++;
            html.Append("<li id='" + common_Area.HelpCategoryID + "' name='" + num.ToString() + "'>");
            html.Append("<a href='javascript:void(0)' id='" + common_Area.Name + "'value='" + common_Area.HelpCategoryID + "'>");
            html.Append("<ins>&nbsp;</ins>");
            html.Append(common_Area.HelpCategoryID + "-" + common_Area.Name);
            html.Append("</a>");
            if (common_Area.Sons != null && common_Area.Sons.Count() > 0)
            {
                html.Append("<ul>");
                foreach (var item in common_Area.Sons)
                {
                    this.GetSubCategory(html, item, num);
                }
                html.Append("</ul>");
            }
            html.Append("</li>");
        }
        #endregion

        #endregion
    }
}
